package com.huiyujinrong.qiangdanbao.activity.login;

import android.content.Context;

import com.huiyujinrong.qiangdanbao.BorrowHelpApp;
import com.huiyujinrong.qiangdanbao.DaggerModelComponent;
import com.huiyujinrong.qiangdanbao.bean.RegisterBean;
import com.huiyujinrong.qiangdanbao.model.RegisterModel;
import com.huiyujinrong.qiangdanbao.network.Api;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import javax.inject.Inject;

import happyloan.core.RespEvent;

public class RegisterPresenter implements RegisterContract.Presenter{
    @Inject
    RegisterModel model;
    private RegisterContract.View view;
    private Context context;

    public RegisterPresenter(RegisterContract.View v,Context context) {
        this.view = v;
        this.context=context;
        setupDagger2();
    }
    private void setupDagger2(){
        DaggerModelComponent.builder().appComponent(BorrowHelpApp.get().getAppComponent()).build().inject(this);
    }

    /**
     * 获取验证码
     * @param tel
     */
    @Override
    public void sendCode(String tel,int code_type) {
        model.sendCode(tel,code_type);
    }

    /**
     * 注册
     * @param tel
     * @param password
     * @param sms
     */
    @Override
    public void register(String tel, String sms, String password) {
        model.register(tel,sms,password);
    }


    @Override
    public void start() {
        EventBus.getDefault().register(this);
    }

    @Override
    public void end() {
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEvent(RespEvent<String> event) {
        if (event.getUrl().equals(Api.rgGetCode)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    //数据返回成功
                    view.showToast("发送成功");
                    MobclickAgent.onEvent(context, "register");
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if(event.isAlert()) {
                        view.showToast(String.valueOf(event.getExtra()));
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void respEventa(RespEvent<RegisterBean> event) {
        if (event.getUrl().equals(Api.register)) {
            switch (event.getCode()) {
                case RespEvent.Code.SUCCEED:
                    RegisterBean t = event.getT();
                    t.getToken();
                    //数据返回成功
                    view.exit();
                    break;
                case RespEvent.Code.FAILURE:
                    //数据返回错误
                    if(event.isAlert()){
                        view.showToast(String.valueOf(event.getExtra()));  //提示错误
                    }
                    break;
                case RespEvent.Code.ERROR:
                    //数据返回异常
                    view.showToast("网络不好,稍后再试");
                    break;
            }
        }
    }
}
